<template>
  <d2-container>
    <template slot="header" v-if="type === 'edit'">资金管理-修改</template>
    <template slot="header" v-if="type === 'copy'">资金管理-复制</template>
    <template slot="header" v-if="type === 'look'">资金管理-查看</template>
    <el-card shadow="never" class="d2-mb titleline">
      <template slot="header" style="height: 66px">
        <span class="title_x el-button--primary fl"></span>
        <p class="title_l fl">主要信息</p>
      </template>
      <el-form
        class="filter-container"
        ref="FYForm"
        :model="FYForm"
        label-width="110px"
      >
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="部门名称">
              <el-select
                v-model="FYForm.department"
                placeholder="请选择"
                style="width: 100%"
                :disabled="type === 'look'"
              >
                <el-option
                  v-for="item in selectDepartsList"
                  :key="item.department"
                  :label="item.name"
                  :value="item.department"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="单据ID">
              <el-input v-model="FYForm.invoices_no" :disabled="type === 'look'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="单据日期">
              <el-date-picker
                v-model="FYForm.date"
                type="date" :disabled="type === 'look'"
                style="width: 140px"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="购销类型">
              <el-select
                v-model="FYForm.direction"
                clearable :disabled="type === 'look'"
                class="filter-item"
                placeholder="请选择"
              >
                <el-option label="销售" value="1"></el-option>
                <el-option label="采购" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="原币币种">
              <el-select
                v-model="FYForm.currency"
                placeholder="请选择" :disabled="type === 'look'"
                style="width: 100%"
              >
                <el-option
                  v-for="item in selectCurrencyList"
                  :key="item.currency"
                  :label="item.name"
                  :value="item.currency"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{item.currency}}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="原币金额">
              <el-input-number
                v-model="FYForm.original_price"
                 size="small"
                :precision="2" :disabled="type === 'look'"
                @input="count1"
                :step="1"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="汇率">
              <el-input-number
               size="small"
                v-model="FYForm.exchange_rate"
                :precision="2" :disabled="type === 'look'"
                @input="count1"
                :step="0.1"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="本币金额">
              <el-input-number
               size="small"
                v-model="FYForm.currency_price"
                disabled
                :step="1"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="客商">
              <el-select
                v-model="FYForm.customer"
                placeholder="请选择" :disabled="type === 'look'"
                style="width: 100%"
              >
                <el-option
                  v-for="item in customerList"
                  :key="item.customer"
                  :label="item.name"
                  :value="item.customer"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.customer}}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="期初标识">
              <el-select
                v-model="FYForm.begin_flag"
                clearable :disabled="type === 'look'"
                class="filter-item"
                placeholder="请选择"
              >
                <el-option label="是" value="1" ></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="已匹配金额">
              <el-input-number
               size="small"
                v-model="FYForm.matched_price"
                :precision="2"
                :step="1"
                disabled
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="未匹配金额">
              <el-input-number
               size="small"
                v-model="FYForm.unmatched_price"
                :precision="2"
                :step="1"
                disabled
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="备注">
              <el-input v-model="FYForm.remark" :disabled="type === 'look'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never" class="d2-mb titleline">
      <template slot="header" style="height: 66px">
        <span class="title_x el-button--primary fl"></span>
        <p class="title_l fl">资金分配</p>
      </template>
      <!-- <el-card shadow="never" class="d2-mb"> -->
      <div class="tl mb10">
        <el-button type="default" size="mini" @click="addRow">
          <d2-icon name="plus" />
          增行
        </el-button>
        <el-button
          type="default"
          size="mini"
          icon="el-icon-delete"
          @click="delData"
        >
          删行
        </el-button>
        <!-- <el-button
          type="default"
          size="mini"
          icon="el-icon-delete"
          @click="countChange"
        >
          计算
        </el-button> -->
      </div>
      <el-table
        :data="fygltableData"
        ref="table"
        tooltip-effect="dark"
        border
        stripe
        :row-class-name="tableRow"
        :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
        style="width: 100%"
        @selection-change="selectRow"
      >
        <el-table-column
          type="selection"
          width="45"
          align="center"
        ></el-table-column>
        <el-table-column
          label="序号"
          type="index"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="资金类型" align="center">
          <template slot-scope="scope">
           <el-select  clearable v-model="scope.row.cap_type" placeholder="请选择" :disabled="type === 'look'">
                   <el-option
                       v-for="item in cap_typeList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
                        <span style="float: left">{{item.label}}</span>
                        <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value}}</span> -->
                     </el-option>
                </el-select>
          </template>
        </el-table-column>
        <el-table-column label="敞口号" align="center" sortable width="145">
          <template slot-scope="scope">
            <el-input
              prop="exposure" :disabled="type === 'look'"
              class="post_name"
              v-model="scope.row.exposure"
              style="width: 60px; margin-right: 5px"
            ></el-input>
            <el-button
              type="primary"
              size="mini" :disabled="type === 'look'"
              plain
              @click="CKIddia(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
        <el-table-column  label="品名" align="center" width="145">
           <template slot-scope="scope">
                <el-input prop="commodity_name" class="commodity_name" v-model="scope.row.commodity_name" disabled></el-input>
           </template>
         </el-table-column>
         <el-table-column  label="敞口数量" align="center" width="145">
                <template slot-scope="scope">
                  <el-input prop="exp_vol" class="exp_vol" v-model="scope.row.exp_vol" type="number" min="1" @input="countChange2" disabled></el-input>
                </template>
              </el-table-column>
        <el-table-column label="匹配金额">
          <template slot-scope="scope">
            <el-input
              class="alloc_amount" :disabled="type === 'look'"
              v-model="scope.row.alloc_amount"
              @input="countChange1"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">
            <el-input
              prop="remark" :disabled="type === 'look'"
              class="post_name"
              v-model="scope.row.remark"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-button
      type="primary"
      size="mini"
      style="float: right; margin-left: 10px"
      @click="handle_FYglChange"
      v-if="type !== 'look'"
      >保存</el-button
    >
    <el-button
      type="primary"
      size="mini"
      style="float: right; margin-left: 10px"
      @click="handle_FH"
      v-if="type === 'look'"
      >返回</el-button
    >
    <!-- <el-button type="default" size="mini" style="float: right">取消</el-button> -->
    <!-- 敞口选择 -->
    <el-dialog :visible.sync="CKIddialogVisible" title="敞口选择" width="78%">
      <el-form
        class="filter-container"
        ref="CKForm"
        :model="CKForm"
        label-width="110px"
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          align="center"
        ></el-table-column>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="敞口号">
              <el-input
                placeholder="请输入内容"
                v-model="CKForm.exposure_no"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="敞口类型">
              <el-select
                clearable
                v-model="CKForm.exp_type"
                class="filter-item"
                placeholder="请选择"
              >
                <el-option label="逐笔" value="0"></el-option>
                <el-option label="净敞口" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="品名">
              <el-input
                placeholder="请输入内容"
                v-model="CKForm.commodity_name"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日期">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="CKForm.startDate"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                ></el-date-picker>
              </el-col>
              <el-col class="line tc" :span="2">~</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="CKForm.endDate"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="">
            <el-button type="primary" size="small" style='margin-top:3px;' @click="handle_CK">
              <d2-icon name="search" /> 查询
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        :data="CKIdList"
        border
        highlight-current-row
        @current-change="handleCurrentChange_ta"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="name" label="品名" sortable></el-table-column>
        <el-table-column
          prop="exposure_no"
          label="敞口号"
          sortable
        ></el-table-column>
        <el-table-column
          prop="exp_type"
          label="敞口类型"
          sortable
          :formatter="formatterET"
        ></el-table-column>
        <el-table-column
          prop="direction"
          label="方向"
          sortable
          :formatter="formatterDirection"
        ></el-table-column>
        <el-table-column
          prop="invoice_vol"
          label="单据数量"
          sortable
        ></el-table-column>
        <el-table-column
          prop="exp_vol"
          label="敞口数量"
          sortable
        ></el-table-column>
        <el-table-column prop="price" label="价格" sortable></el-table-column>
        <el-table-column prop="brand" label="品牌" sortable></el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange1"
        background
        :current-page="CKForm.currentPage"
        :page-size="CKForm.pageSize"
        layout="total, prev, pager, next"
        :total="CKForm.totalPage"
      >
      </el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button @click="CKIddialogVisible = false">取消</el-button>
        <el-button type="primary" @click="CKIdChange">保存</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
import {
  conExposure,
  addExposure,
  conspotreddashed,
  addspotreddashed,
  calculate,
  conspotCap,
  addspotCap,
  conCurrency,
  concustomer,
  conFeetype
} from "../../../../../api/url_lcc";
import { getDepartments } from "../../../../../api/url";
export default {
  name: "add_cost",
  //   components: {
  //     "trade-name": tradeName
  //   },
  data() {
    return {
      customerList: [],
      selectDepartsList: [],
      FYForm: {},
      CKIdList: [],
      fygltableData: [],
      type: "",
      selectCurrencyList: [],
      CKForm: {
        page: 1, //当前页
        pageSize: 20,
        totalPage: 1,
        confirm_status: "1",
        commodity_name: "",
        exp_type: "",
        direction: "",
        deal_no: "",
        direction: "",
        base_price_name: "",
        startDate: "",
        endDate: "",
        department_name: "",
        exposure_no: "",
        salesman: "",
        depot: "",
        brand: "",
        origin: "",
        depot: "",
        standard: "",
      },
      tableData: [],
      selectlistRow: [],
      rowNum: 1,
      CKIddialogVisible: false,
      row: "",
      asd2: "",
      cap_typeList:[
        {
          value: 0,
          label:'货款',
        },
         {
          value: 1,
          label:'资金划拨',
        },
         {
          value: 2,
          label:'利息',
        }
      ]
    };
  },
  created() {
    this.tabledatas = [
      { title: "标题1", text: "s111sssa" },
      { title: "标题2", text: "ss222ssa" },
    ];
    this.tabledatas.map((i) => {
      i.show = false;
      return i;
    });
  },
  // created(){
  //   this.editListAll()
  // },
  methods: {
    // 本币金额
    count1() {
      var original_price = this.FYForm.original_price;
      var exchange_rate = this.FYForm.exchange_rate;
      this.FYForm.currency_price = original_price * exchange_rate;
    },
    //  部门信息
    //树形结构转换为扁平数组
    treeToList(tree) {
      var queen = [];
      var out = [];
      queen = queen.concat(tree);
      while (queen.length) {
        var first = queen.shift();
        if (first.sub_depart) {
          queen = queen.concat(first.sub_depart);
          delete first["sub_depart"];
        }
        out.push(first);
      }
      return out;
    },
    // 查询部门
    handle_getDepartments() {
      getDepartments()
        .then((res) => {
          this.selectDepartsList = [];
          var data1 = res.result;
          var arr = this.treeToList(data1);
          for (var i = 0; i < arr.length; i++) {
            this.selectDepartsList[i] = {
              name: arr[i]["name"],
              department: arr[i]["id"],
            };
            // console.log(1, typeof this.selectDepartsList[0].department);
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },
    //  客商信息
    handle_customerChange() {
      concustomer(this.cusForm)
        .then((res) => {
          this.customerList = [];
          var data = res.result;
          for (var i = 0; i < data.length; i++) {
            this.customerList[i] = {
              customer: data[i].id,
              name: data[i].name,
            };
          }
          console.log(1, data, 2, this.customerList);
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },
  
    // 敞口类型逐笔，净敞口
    formatterET(row, column) {
      if (row.exp_type == "0") {
        return "逐笔";
      } else if (row.exp_type == "1") {
        return "净敞口";
      }
    },
    //  购销类型
    formatterDirection(row, column) {
      if (row.direction == "0") {
        return "采购";
      } else if (row.direction == "1") {
        return "销售";
      }
    },
    // 行内编辑
    handleCellDataChange({ rowIndex, key, value, row }) {
      debbuger;
      console.log(rowIndex);
      console.log(key);
      console.log(value);
      console.log(row);
    },

    // 单选
    handleCurrentChange(currentRow, oldCurrentRow) {
      console.log(currentRow);
      console.log(oldCurrentRow);
    },
    // 多选
    handleSelectionChange(selection) {
      console.log(selection);
    },
    // 获取表格选中时的数据
    selectRow(val) {
      this.selectlistRow = val;
    },
    // 增加行
    addRow() {
      var list = {
        rowNum: this.rowNum,
        cap_type: "",
        exposure: "",
        alloc_amount: "",
        remark: "",
      };
      this.fygltableData.unshift(list);
      this.rowNum += 1;
    },
    // 删除方法
    // 删除选中行
    delData() {
      for (let i = 0; i < this.selectlistRow.length; i++) {
        let val = this.selectlistRow;
        val.forEach((val, index) => {
          this.fygltableData.forEach((v, i) => {
            if (val.rowNum === v.rowNum) {
              // i 为选中的索引
              this.fygltableData.splice(i, 1);
            }
          });
        });
      }
      // 删除完数据之后清除勾选框
      this.$refs.table.clearSelection();
    },
    // 获取传过来的参数
    editListAll() {
      console.log(
        "获取传过来的参数fund",
        this.$route.params.type,
        this.$route.params.editList
      );
      this.fygltableData = [];
      var data = this.$route.params.editList[0].spot_alloc_cap;
      this.fygltableData = data;
      //  for (var i = 0; i < data.length; i++) {
      //     this.fygltableData[i] = {
      //       alloc_amount: data[i].alloc_amount,
      //       cap_type: data[i].cap_type.toString(),
      //       cap_type_name: data[i].cap_type_name,
      //       exposure: data[i].exposure,
      //       id: data[i].id,
      //       remark: data[i].remark,
      //       spot_fee: data[i].spot_fee,
      //     };
      //   }

      this.type = this.$route.params.type;
      this.FYForm = {
        department: this.$route.params.editList[0].department,
        date: this.$route.params.editList[0].date,
        customer: this.$route.params.editList[0].customer,
        currency: this.$route.params.editList[0].currency,
        direction: this.$route.params.editList[0].direction.toString(),
        virtual: this.$route.params.editList[0].virtual,
        original_price: this.$route.params.editList[0].original_price,
        begin_flag: this.$route.params.editList[0].begin_flag.toString(),
        exchange_rate: this.$route.params.editList[0].exchange_rate,
        currency_price: this.$route.params.editList[0].currency_price,
        remark: this.$route.params.editList[0].remark,
        matched_price: this.$route.params.editList[0].matched_price,
        unmatched_price: this.$route.params.editList[0].unmatched_price,
        invoices_no: this.$route.params.editList[0].invoices_no,

      };
      console.log(
        211,
        this.$route.params.editList[0].department,
        32,
        this.$route.params.editList[0].department.toString()
      );
    },
    // // 计算
      handle_count(){
         let data = this.fygltableData
         var data2 = ""
        data.forEach(function(item, index) {
           var data2 = item.alloc_amount
           console.log(112122,data2)
           var data2 = data2
        })
        if(data2 == ""){
           this.countChange2()
        }else{
          this.countChange1()
        }
      },
    // 计算匹配金额----修改匹配金额
      countChange1(){
         if(this.FYForm.currency_price == ""){
          this.$message.error("请先输入原币金额和汇率！");
          this.readonly=true;
        }else{
          this.readonly=false;
          let data = this.fygltableData
          var Newdata  = [];
          data.forEach(function(item, index) {
              var data1 = item.alloc_amount
              Newdata.push(Number(data1));
              console.log(90,data1,91,Newdata,92,typeof(data1))
           })
           let params = {
               operation: "FEE",
               "alloc_amount": Newdata,
               "currency_price":this.FYForm.currency_price
           };
        calculate(params).then(res => {
                 var data = res.result
                 this.FYForm = {
                     currency_price: data.currency_price,
                     matched_price: data.matched_price,     
                     unmatched_price: data.unmatched_price, 
                     department: this.FYForm.department,
                     date: this.FYForm.date,
                     customer: this.FYForm.customer,
                     currency: this.FYForm.currency,
                     direction: this.FYForm.direction,
                     virtual: this.FYForm.virtual,
                     original_price: this.FYForm.original_price,
                     begin_flag: this.FYForm.begin_flag,
                     exchange_rate: this.FYForm.exchange_rate,
                     remark: this.FYForm.remark, 
                 }
            }).catch(err =>{
          this.$message.error(err.msg);
        });
        }
        },
      // 计算匹配金额----不修改匹配金额
      countChange2(){
        let params = {
            operation: "FEE",
            fee:{
              currency_price:this.FYForm.currency_price,
              data:this.fygltableData
            } 
        };
        calculate(params).then(res => {
                 var data = res.result
                 console.log(12,data)
                 this.FYForm = {
                     currency_price: data.fee.currency_price,
                     matched_price: data.matched_price,     
                     unmatched_price: data.unmatched_price, 
                     department: this.FYForm.department,
                     date: this.FYForm.date,
                     customer: this.FYForm.customer,
                     currency: this.FYForm.currency,
                     direction: this.FYForm.direction,
                     virtual: this.FYForm.virtual,
                     original_price: this.FYForm.original_price,
                     begin_flag: this.FYForm.begin_flag,
                     exchange_rate: this.FYForm.exchange_rate,
                     remark: this.FYForm.remark, 
                 }
                 console.log(10,data.fee.data)
                 this.fygltableData = data.fee.data
            }).catch(err =>{
          this.$message.error(err.msg);
        });
        },
    // 修改保存
    edit_FYglChange() {
      console.log('cap_type',this.fygltableData)
        if(this.FYForm.unmatched_price<0){
          this.$message.error("已匹配金额不能大于本币金额！");
        }else{
      let data = this.fygltableData
       var Newdata  = [];
       var asd2 = "";
       data.forEach(function(item, index) {
           var data1 = item.cap_type
            console.log(100,data1,data1== 0,6,typeof(data1))
         if(data1 == 0) {
             var cap_type = '货款'
             var cap_type_name = '货款'
             console.log(90,cap_type_name)
         }else if (data1 == 1) {
             var cap_type = '资金划拨'
             var cap_type_name = '资金划拨'
         }else if (data1 == 2) {
             var cap_type = '利息'
             var cap_type_name = '利息'
         }
        //  else if (data1 == "3") {
        //      var cap_type = '运输费';
        //  } else if (data1 == "4") {
        //      var cap_type = '装卸费';
        //  } else if (data1 == "5") {
        //      var cap_type = '仓储费';
        //  } else if (data1 == "6") {
        //      var cap_type = '保险费';
        //  } else if (data1 == "7") {
        //      var cap_type = '质押费';
        //  } else if (data1 == "8") {
        //      var cap_type = '清关费';
        //  } 
        
           Newdata.push(cap_type);
            console.log(2,cap_type)
           var last=JSON.stringify(Newdata);
           var asd = last.replace(/"/g, "");
           var asd1 = asd.replace('[','');
           asd2 = asd1.replace(']','');
        })
        console.log(2,asd2)
      let params = {
        operation: "MOD",
        spot_cap: {
          department: this.FYForm.department,
          date: this.FYForm.date,
          direction: this.FYForm.direction,
          currency: this.FYForm.currency,
          original_price: this.FYForm.original_price,
          exchange_rate: this.FYForm.exchange_rate,
          currency_price: this.FYForm.currency_price,
          customer: this.FYForm.customer,
          begin_flag: this.FYForm.begin_flag,
          remark: this.FYForm.remark,
          matched_price: this.FYForm.matched_price,
          unmatched_price: this.FYForm.unmatched_price,
          cap_type_all: asd2,
          is_settlement: 0,
          status: 0,
          invoices_no: this.$route.params.editList[0].invoices_no,
        },
        spot_alloc_fee: this.fygltableData,
      };
      addspotCap(params)
        .then((res) => {
          this.$message.success(res.result);
          this.$router.push('/arbitrage_tool/cash_management?table=second');
          this.closeing.closePage(this.$store.getters["d2admin/page/allPage"],this.$store.getters["d2admin/page/currentPage"])
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
        }
    },
    // 复制保存
    copy_FYglChange() {
        if(this.FYForm.unmatched_price<0){
          this.$message.error("已匹配金额不能大于本币金额！");
        }else{
      this.countChange2();
      let data = this.fygltableData
       var Newdata  = [];
       var asd2 = "";
       data.forEach(function(item, index) {
           var data1 = item.cap_type
         if (data1 == "0") {
             var cap_type = '货款';
         } else if (data1 == "1") {
             var cap_type = '资金划拨';
         } else if (data1 == "2") {
             var cap_type = '利息';
         } else if (data1 == "3") {
             var cap_type = '运输费';
         } else if (data1 == "4") {
             var cap_type = '装卸费';
         } else if (data1 == "5") {
             var cap_type = '仓储费';
         } else if (data1 == "6") {
             var cap_type = '保险费';
         } else if (data1 == "7") {
             var cap_type = '质押费';
         } else if (data1 == "8") {
             var cap_type = '清关费';
         }
           Newdata.push(cap_type);
           var last=JSON.stringify(Newdata);
           var asd = last.replace(/"/g, "");
           var asd1 = asd.replace('[','');
           asd2 = asd1.replace(']','');
        })
      let params = {
        operation: "ADD",
        spot_cap: {
          department: this.FYForm.department,
          date: this.FYForm.date,
          direction: this.FYForm.direction,
          currency: this.FYForm.currency,
          original_price: this.FYForm.original_price,
          exchange_rate: this.FYForm.exchange_rate,
          currency_price: this.FYForm.currency_price,
          customer: this.FYForm.customer,
          begin_flag: this.FYForm.begin_flag,
          remark: this.FYForm.remark,
          matched_price: this.FYForm.matched_price,
          unmatched_price: this.FYForm.unmatched_price,
          cap_type_all: asd2,
        },
        spot_alloc_fee: this.fygltableData,
      };
      addspotCap(params)
        .then((res) => {
          this.$message.success(res.result);
          this.$router.push('/arbitrage_tool/cash_management?table=second');
          this.closeing.closePage(this.$store.getters["d2admin/page/allPage"],this.$store.getters["d2admin/page/currentPage"])  
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
        }
    },
    // 返回跳转
    handle_FH(){
      this.$router.push('/arbitrage_tool/cash_management?table=second');
      this.closeing.closePage(this.$store.getters["d2admin/page/allPage"],this.$store.getters["d2admin/page/currentPage"])
    },
    // 保存
    handle_FYglChange() {
      if (this.$route.params.type === "copy") {
        this.copy_FYglChange();
      } else if (this.$route.params.type === "edit") {
        this.edit_FYglChange();
      }
    },
    //  敞口选择弹框
    CKIddia(index, row) {
      this.CKIddialogVisible = true;
      this.CKForm.page = 1;
      // this.FYGLChange();
      this.handle_currencyChange();
      conExposure(this.CKForm)
        .then((res) => {
          this.CKIdList = [];
          this.CKForm.direction = this.FYForm.direction;
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
            // this.CKIdList[i].exposure_no = this.CKIdList[i].exposure_no;
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
      this.index_s = index;
    },
    // 查询币种
    handle_currencyChange() {
      let params = {
        name: "",
      };
      conCurrency(params)
        .then((res) => {
          this.selectCurrencyList = [];
          var data1 = res.result;
          console.log(902);
          for (var i = 0; i < data1.length; i++) {
            this.selectCurrencyList[i] = {
              currency: data1[i]["id"],
              name: data1[i]["name"],
            };
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },
    // 敞口选择查询
    // 每页数
    handleSizeChange(val) {
      this.CKForm.pageSize = val;
      this.CKForm.page = 1;
      this.FYGLChange();
    },
    // 当前页
    handleCurrentChange1(val) {
      this.CKForm.page = val;
      this.FYGLChange();
    },
    // 查询
    handle_CK() {
      this.CKForm.page = 1;
      // this.FYGLChange();
      this.CKForm.direction = this.FYForm.direction;
      this.handle_currencyChange();
      conExposure(this.CKForm)
        .then((res) => {
          this.CKIdList = [];
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
      this.index_s = index;
    },
    FYGLChange() {
      conExposure(this.CKForm)
        .then((res) => {
          this.CKIdList = [];
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },
    // 单选
    handleCurrentChange_ta(val) {
      this.currentRow = val;
    },
    // 保存敞口
    CKIdChange(index) {
      this.CKIddialogVisible = false;
      console.log("保存敞口", this.currentRow);
      console.log("index", this.fygltableData, 12, this.index_s);
      this.fygltableData[this.index_s.index].exposure = this.currentRow.exposure_no;
      this.fygltableData[this.index_s.index].exp_vol = this.currentRow.exp_vol;
       this.fygltableData[this.index_s.index].commodity_name = this.currentRow.name;
      this.dialogVisible_one = false;
      this.countChange2();
    },
    tableRow({ row, rowIndex }) {
      row.index = rowIndex;
    },
  },
  mounted() {
    this.handle_getDepartments();
    this.handle_customerChange();
    this.editListAll();
    this.handle_currencyChange();
    this.handlefee_typeChange();
  },
};
</script>
